#!/usr/bin/env bash
set -Eeo pipefail

USER="$(id -u)"

create_directory_if_not_exists() {
    local dir="$1"
    if [ ! -e "$dir" ]; then
        mkdir "$dir"
    fi
}

create_directory_if_not_exists "/home/rocketmq/conf"
create_directory_if_not_exists "/home/rocketmq/logs"
create_directory_if_not_exists "/home/rocketmq/store"

if [ ! -e /home/rocketmq/conf/broker.conf ]; then
        cp /opt/rocketmq/conf/broker.conf /home/rocketmq/conf/broker.conf
fi

if [ "$USER" -eq 0 ]; then
        chown -R rocketmq:rocketmq /home/rocketmq/store /home/rocketmq/logs /home/rocketmq/conf
fi

exec gosu rocketmq "$@"
